package mt.controller;

import lombok.RequiredArgsConstructor;
import mt.entity.Ingredient;
import mt.service.IngredientService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class IngredientController {
    private final IngredientService service;

    @GetMapping("/ingredientList")
    public String toIngredientList(Model model) {
        List<Ingredient> list = service.list();
        model.addAttribute("list", list);
        return "ingredientList";
    }

    @GetMapping("/ingredientCreate")
    public String toUserCreate() {
        return "/ingredientCreate";
    }

    @GetMapping("/ingredientUpdate")
    public String toIngredientUpdate(Integer id, Model model) {
        Ingredient ingredient = service.getById(id);
        model.addAttribute("ingredient", ingredient);
        return "ingredientUpdate";
    }




    @PostMapping("/ingredientList")
    public String ingredientList(String name, Model model) {
        List<Ingredient> list = service.listByName(name);
        model.addAttribute("name",name);
        model.addAttribute("list", list);
        return "ingredientList";
    }

    @PostMapping("/ingredientCreate")
    public String ingredientCreate(String name, Integer stock) {
        Ingredient ingredient = new Ingredient();
        ingredient.setName(name);
        ingredient.setStock(stock);
        service.save(ingredient);
        return "redirect:ingredientList";
    }

    @PostMapping("/ingredientUpdate")
    public String ingredientUpdate( Ingredient ingredient) {
        service.updateById(ingredient);
        return "redirect:ingredientList";
    }

    @GetMapping("/ingredientRemove")
    public String ingredientRemove( Integer id) {
        service.removeById(id);
        return "redirect:ingredientList";
    }

}
